if ~exist('sim_mode_range', 'var')
  sim_mode_range = 1:3;
end

if ~exist('random_seed_range', 'var')
  random_seed_range = 1:100; % Got up to 3 so can set to 4.
end

if ~exist('sched_mode_range', 'var')
  sched_mode_range = 1:3;
end

date_string = datestr(now,'yyyymmddTHHMMSS_FFF');
filename = sprintf('sim_%s_log.csv', date_string);
logfile = fopen(filename, 'at');
fprintf(logfile,'sim_mode, random_seed, sched_mode, nr_bsmf_miss, nr_bsmf_meas, nr_bsmf_sched, nr_fused_tracks, nr_cand_tracks_radar1, nr_cand_tracks_radar2, nr_hist_tracks_radar1, nr_hist_tracks_radar2, nr_fail_tracks_radar1, nr_fail_tracks_radar2\n');
fclose(logfile);


for sim_mode=sim_mode_range
  for random_seed=random_seed_range
    for sched_mode=sched_mode_range
      % Clean up
      clearvars -except sim_mode_range random_seed_range sched_mode_range date_string filename sim_mode random_seed sched_mode

      % Execute the next simulation run
      plot_on = 0;
      Framework

      % Log the file
      while 1
        logfile = fopen(filename, 'at');
        if logfile ~= -1
          break;
        end
      end
      
      fprintf(logfile,'%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n', sim_mode, random_seed, sched_mode, sum(nr_bsmf_miss), sum(nr_bsmf_poss), sum([fused_tracks(:).bsmf_poss]), length(fused_tracks), length(cand_tracks{1}), length(cand_tracks{2}), length(hist_tracks{1}), length(hist_tracks{2}), length(fail_tracks{1}), length(fail_tracks{2}));    
      fclose(logfile);

      if (nr_intercepts_miss ~= 0) || (nr_radar_too_fast ~= 0) || (nr_radar_out_of_bounds ~= 0)
        filename2 = sprintf('sim_%s_vars.%d_%d.mat', date_string, random_seed, sched_mode);
        save(filename2);
      end
    end
  end
end